package com.sugar.queryinfo.module.business.controller.admin.userauth;

import com.sugar.queryinfo.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.queryinfo.module.business.util.HideUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.controller.admin.userauth.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.userauth.UserAuthDO;
import com.sugar.queryinfo.module.business.service.userauth.UserAuthService;

@Tag(name = "管理后台 - 用户授权维护")
@RestController
@RequestMapping("/business/user-auth")
@Validated
public class UserAuthController {

    @Resource
    private UserAuthService userAuthService;

    @PostMapping("/create")
    @Operation(summary = "创建用户授权维护")
    @PermitAll
    public CommonResult<Long> createUserAuth(@Valid @RequestBody UserAuthSaveReqVO createReqVO) {
        return success(userAuthService.createUserAuth(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户授权维护")
    @PreAuthorize("@ss.hasPermission('business:user-auth:update')")
    public CommonResult<Boolean> updateUserAuth(@Valid @RequestBody UserAuthUpdateReqVO updateReqVO) {
        userAuthService.updateUserAuth(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户授权维护")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-auth:delete')")
    public CommonResult<Boolean> deleteUserAuth(@RequestParam("id") Long id) {
        userAuthService.deleteUserAuth(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户授权维护")
                @PreAuthorize("@ss.hasPermission('business:user-auth:delete')")
    public CommonResult<Boolean> deleteUserAuthList(@RequestParam("ids") List<Long> ids) {
        userAuthService.deleteUserAuthListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户授权维护")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-auth:query')")
    public CommonResult<UserAuthRespVO> getUserAuth(@RequestParam("id") Long id) {
        UserAuthDO userAuth = userAuthService.getUserAuth(id);
        return success(BeanUtils.toBean(userAuth, UserAuthRespVO.class, respVO ->
                respVO.setName(HideUtil.name(respVO.getName()))
                        .setIdCard(HideUtil.idCard(respVO.getIdCard()))
                        .setMobile(HideUtil.mobile(respVO.getMobile()))));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户授权维护分页")
    @PreAuthorize("@ss.hasPermission('business:user-auth:query')")
    public CommonResult<PageResult<UserAuthRespVO>> getUserAuthPage(@Valid UserAuthPageReqVO pageReqVO) {
        PageResult<UserAuthDO> pageResult = userAuthService.getUserAuthPage(pageReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(BeanUtils.toBean(pageResult, UserAuthRespVO.class, respVO ->
                respVO.setName(HideUtil.name(respVO.getName()))
                        .setIdCard(HideUtil.idCard(respVO.getIdCard()))
                        .setMobile(HideUtil.mobile(respVO.getMobile()))));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户授权维护 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-auth:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserAuthExcel(@Valid UserAuthPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserAuthDO> list = userAuthService.getUserAuthPage(pageReqVO, SecurityFrameworkUtils.getLoginUserId()).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户授权维护.xls", "数据", UserAuthRespVO.class,
                        BeanUtils.toBean(list, UserAuthRespVO.class));
    }

}